package com.fg.estate.controller;

import com.fg.estate.bean.Permission;
import com.fg.estate.service.PermissionService;
import com.fg.estate.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * @author 丰哥
 * 2022/10/12
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/listUi")
    public String listUi(ModelMap map) {

        try {
            List<Map<String, Object>> rolePermissions = roleService.selectRoleAndPermissions();
            map.addAttribute("rolePermissions", rolePermissions);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "role_list";
    }

    // 添加角色画面
    @RequestMapping("/addUi")
    public String addUi(ModelMap modelMap) {

        try {
            List<Permission> permissions = permissionService.selectAll();
            modelMap.put("permissions", permissions);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "role_add";
    }

    // 添加角色
    @RequestMapping("/add")
    public String add(String rname, long[] pids) {

        try {
            roleService.insertRole(rname, pids);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/role/addUi";
    }
}
